package com.justech.mes.v2.dao.repository;

import com.justech.mes.v2.model.MesEquipBasic;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author feifei.li
 */
@Repository
public interface MesEquipBasicRepository extends CrudRepository<MesEquipBasic, Long> {
    /**
     * 根据类型查询数据
     *
     * @param dataType
     * @param equipmentName
     * @param sn
     * @param isRepeat
     * @return
     */
    List<MesEquipBasic> findAllByDataTypeAndEquipmentNameAndSnAndIsRepeatOrderByIndex(String dataType, String equipmentName, String sn, int isRepeat);

    /**
     * 查看是否重复提交
     *
     * @param index
     * @param isOk
     * @return
     */
    Integer countByIndexAndIsOkAndEquipmentNameAndSnAndDataType(Integer index, Integer isOk, String equipmentName, String sn, String dataType);


    /**
     * @param equipmentName
     * @param sn
     * @param dataType
     * @return
     */
    @Transactional
    @Query(value = "update mes_v2_equipment_basic set is_ok=1 where equipment_name=?1 and sn=?2 and data_type=?3", nativeQuery = true)
    @Modifying
    Integer updateIsOk(String equipmentName, String sn, String dataType);

    /**
     * 查询是否有数据需要修改
     *
     * @param equipmentName
     * @param sn
     * @param dataType
     * @return
     */
    List<MesEquipBasic> findAllByEquipmentNameAndSnAndDataType(String equipmentName, String sn, String dataType);

    /**
     * 删除没有完成的数据
     *
     * @param equipmentName
     * @param isOk
     * @param sn
     * @param dataType
     * @return
     */
    List<MesEquipBasic> findAllByEquipmentNameAndIsOkNotAndSnAndDataTypeNot(String equipmentName, int isOk, String sn, String dataType);

    /**
     * 查询是否重复数据
     *
     * @param equipmentName
     * @param sn
     * @param dataType
     * @param index
     * @param isRepeat
     * @return
     */
    List<MesEquipBasic> findByEquipmentNameAndSnAndDataTypeAndIndexAndIsRepeat(String equipmentName, String sn, String dataType, Integer index, Integer isRepeat);
}
